package com.jl.easybuysys.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jl.easybuysys.mapper.EbNewsMapper;
import com.jl.easybuysys.mapper.EbProductCategoryMapper;
import com.jl.easybuysys.model.EbNews;
import com.jl.easybuysys.model.EbProduct;
import com.jl.easybuysys.model.EbProductCategory;
import com.jl.easybuysys.service.EbNewsService;
import com.jl.easybuysys.service.EbProductCategoryService;
import com.jl.easybuysys.service.EbProductCategoryService;
import com.jl.easybuysys.service.EbProductService;

import com.jl.easybuysys.service.EbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private EbProductService ebProductService;
    @Autowired
    private EbNewsService ebNewsService;
    @Autowired
    private EbProductCategoryService ebProductCategoryService;

    @RequestMapping("/front")
    public String index(@RequestParam(defaultValue ="1") int psize,
                        @RequestParam(defaultValue = "8") int pageSize,
                        @RequestParam(defaultValue ="1") int psizes,
                        @RequestParam(defaultValue = "8") int pageSizes,
                        Model model, HttpServletRequest request){
        // System.out.println("访问前台首页");

        //TODO 1.分页商品
        Page<EbProduct> productPage = ebProductService.list(psize,pageSize);
        //TODO 2.最新的新闻数据
        Page<EbNews> newsPage = ebNewsService.list(psizes,pageSizes);
        //TODO 3.商品分类数据
        Map<String, List<EbProductCategory>> cateMap = ebProductCategoryService.selectBigAndSmallCate();
        //TODO 4.将数据绑定到model中
        model.addAttribute("productPage",productPage);//绑定商品数据
        model.addAttribute("newsPage",newsPage);//绑定新闻数据
//        model.addAttribute("cateMap",cateMap);//绑定分类数据
        //将分类数据放入到session中，才能在多个请求中传递
        request.getSession().setAttribute("cateMap",cateMap);
        //TODO 5.指定视图（view）--html
        return "index";//templates/index.html
    }

}
